package com.alibaba.citrus.cr.platform.enhance.facade.service;

import java.util.Arrays;
import java.util.Objects;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.api.datapermission.service.DataPermissionService;
import com.epoch.app.crplatformenhance.enums.OrganizationTypeEnum;
import com.epoch.app.crplatformenhance.model.dto.DimensionDTO;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * @author luyi
 * @date 2021/10/16 2:41 下午
 */
@Primary
@Service
public class DataPermissionServiceImpl implements DataPermissionService {
    @Override
    @FacadeInvoker
    public Result<Boolean> salesPlanData(DimensionDTO dimensionDTO) {
        if (dimensionDTO.getApiContext().getIsSales() != null && dimensionDTO.getApiContext().getIsSales()) {
            if (dimensionDTO != null && dimensionDTO.getBizType() != null) {
                throw new FacadeException("OTS-01-003-01-16-002");
            }
            return Result.success(true);
        }
        OrganizationTypeEnum typeEnum =
                Arrays.stream(OrganizationTypeEnum.values()).filter(source -> dimensionDTO.getBizType().equals(source.getCode())).findFirst().orElse(null);
        if (Objects.isNull(typeEnum)) {
            throw new FacadeException("OTS-01-003-01-16-001");
        }
//        if (apiContext.getRelatedOrganizationType() > dimensionDTO.getBizType()){
//            throw new FacadeException("OTS-01-003-01-16-002");
//        }
        if (dimensionDTO.getApiContext().getRelatedOrganizationType().equals(dimensionDTO.getBizType())) {
            if (!dimensionDTO.getApiContext().getBelongOrganizationId().equals(dimensionDTO.getBizId())) {
                throw new FacadeException("OTS-01-003-01-16-002");
            }
        }
        return Result.success(true);
    }
}
